package com.hyt.it.ogt.kq.common.gov.enums;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.hyt.it.ogt.kq.common.gov.constant.admission.AdmissionTicketConstant;
import com.hyt.it.ogt.kq.common.gov.constant.admission.AdmissionTicketTemplateCode;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 配置项枚举
 *
 * @author zhoumiao
 * @since 2023/1/10 10:48
 **/
@Slf4j
public enum CollectInfoEnum {
    NAME(AdmissionTicketTemplateCode.NAME, "姓名"),
    SEX(AdmissionTicketTemplateCode.SEX, "性别"),
    TELEPHONE(AdmissionTicketTemplateCode.TELEPHONE, "固定电话"),
    CERTIFICATE_TYPE(AdmissionTicketTemplateCode.CERTIFICATE_TYPE, "证件类型"),
    ID_CARD(AdmissionTicketTemplateCode.ID_CARD, "证件号码"),
    ID_CARD_PHOTO(AdmissionTicketTemplateCode.ID_CARD_PHOTO, "身份证照片"),
    CERTIFICATE_PHOTO(AdmissionTicketTemplateCode.CERTIFICATE_PHOTO, "证件照"),
    BIRTHDAY(AdmissionTicketTemplateCode.BIRTHDAY, "出生日期"),
    AGE(AdmissionTicketTemplateCode.AGE, "年龄"),
    PHONE(AdmissionTicketTemplateCode.PHONE, "手机号"),
    EMAIL(AdmissionTicketTemplateCode.EMAIL, "邮箱"),
    OFFICE(AdmissionTicketTemplateCode.OFFICE, "组织机构"),
    AREA(AdmissionTicketTemplateCode.AREA, "地区"),
    ADMISSION_NO(AdmissionTicketTemplateCode.ADMISSION_NO, "准考证号"),
    ADDRESS(AdmissionTicketTemplateCode.ADDRESS, "居住地址"),
    ;
    public static final int addSort = 100;

    private String code;
    private String text;

    CollectInfoEnum(String code, String text) {
        this.code = code;
        this.text = text;
    }

    public String getCode() {
        return code;
    }

    public String getText() {
        return text;
    }

    public static CollectInfoEnum getByCode(String componentName) {
        return Arrays.stream(CollectInfoEnum.values())
                .filter(collectInfoEnum -> collectInfoEnum.getCode().equals(componentName))
                .findFirst()
                .orElse(null);
    }

    public static boolean isCollectInfo(String code) {
        return Arrays.stream(CollectInfoEnum.values()).anyMatch(collectInfoEnum -> Objects.equals(collectInfoEnum.getCode(), code));
    }

    /**
     * 转换自定义配置信息的code为英文
     *
     * @param componentName
     * @return
     */
    public static String convertCode(String componentName, String title) {
        String code;
        if (CollectInfoEnum.isCollectInfo(componentName)) {
            code = componentName;
        } else {
            // 自定义文本可能有中文，通过title转换成拼音
            code = PinyinUtil.getPinyin(title, "");

            String pyCode = getPyCode(code);
            if (StrUtil.isNotBlank(pyCode)) {
                code = pyCode;
            }
        }
        return code;
    }

    /**
     * 获取拼音的最后code，英文字母、数字和下划线
     *
     * @param pyCode
     * @return
     */
    public static String getPyCode(String pyCode) {
        StringBuilder matcherCode = new StringBuilder("py_");

        // 编译正则表达式
        Pattern pattern = Pattern.compile(AdmissionTicketConstant.PINYIN_REGEX);
        // 创建 Matcher 对象
        Matcher matcher = pattern.matcher(pyCode);
        // 循环匹配
        while (matcher.find()) {
            // 获取匹配到的内容
            String match = matcher.group();
            log.trace("getPyCode 匹配到 Match: {}", match);
            matcherCode.append(match);
        }
        return matcherCode.toString();
    }
}
